#ifndef SCENE_H
#define SCENE_H

#include <QGraphicsScene>
#include <QGraphicsSceneMouseEvent>
#include <QKeyEvent>
#include <QMediaPlayer>
#include <QMediaDevices>
#include <QAudioDevice>
#include <QAudioOutput>
#include "cardtable.h"

class BottomButton : public QObject, public QGraphicsPixmapItem
{
    Q_OBJECT
public:
    BottomButton(const QString &text, QObject *parent = nullptr): QObject{parent}
    {
        setTransformationMode(Qt::SmoothTransformation);
        setPixmap(QPixmap(":/textures/bottom.png"));
        QGraphicsTextItem *item = new QGraphicsTextItem(text, this);
        item->setFont(QFont("Microsoft YaHei", 10));
        item->setDefaultTextColor(Qt::white);
        QPointF offset(item->boundingRect().width()/ 2, item->boundingRect().height() / 2);
        item->setPos(boundingRect().center() - offset);
        setAcceptHoverEvents(true);
        setCacheMode(DeviceCoordinateCache);
        disabled = false;
    }
    void setDisabled(bool disable)
    {
        disabled = disable;
        if (disable)
            setPixmap(QPixmap(":/textures/bottom_disabled.png"));
        else
            setPixmap(QPixmap(":/textures/bottom.png"));
    }

private:
    bool disabled;
    bool left_pressed;

signals:
    void clicked();

protected:
    void hoverEnterEvent(QGraphicsSceneHoverEvent *e) override
    {
        if (disabled) return;
        setPixmap(QPixmap(":/textures/bottom_hover.png"));
    }
    void hoverLeaveEvent(QGraphicsSceneHoverEvent *e) override
    {
        if (disabled) return;
        setPixmap(QPixmap(":/textures/bottom.png"));
    }
    void mousePressEvent(QGraphicsSceneMouseEvent *e) override
    {
        if (disabled) return;
        if (e->button() != Qt::LeftButton) return;
        left_pressed = true;
    }
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *e) override
    {
        if (disabled) return;
        if (e->button() != Qt::LeftButton) return;
        if (left_pressed)
        {
            left_pressed = false;
            emit clicked();
        }
    }
};

class WinCountLabel : public QGraphicsPixmapItem
{
public:
    WinCountLabel(uint32_t count = 0) : QGraphicsPixmapItem()
    {
        setTransformationMode(Qt::SmoothTransformation);
        setPixmap(QPixmap(":/textures/win_count.png"));
        QGraphicsTextItem *title = new QGraphicsTextItem("胜利次数计数", this);
        title->setFont(QFont("黑体", 10));
        title->setDefaultTextColor(Qt::black);
        QPointF offset(title->boundingRect().width()/ 2, title->boundingRect().height() / 2);
        title->setPos(QRectF(65, 7, 117, 19).center() - offset);

        count_label = new QGraphicsTextItem(QString::number(count), this);
        count_label->setFont(QFont("Microsoft YaHei", 9));
        count_label->setDefaultTextColor(Qt::white);
        offset = QPointF(count_label->boundingRect().width()/ 2, count_label->boundingRect().height() / 2);
        count_label->setPos(QRectF(191, 8, 88, 17).center() - offset);
    }
    void setWinCount(uint32_t count)
    {
        count_label->setPlainText(QString::number(count));
    }

private:
    QGraphicsTextItem *count_label;
};

class Scene : public QGraphicsScene
{
    Q_OBJECT
public:
    Scene(qreal x, qreal y, qreal width, qreal height,QObject *parent = nullptr);
    virtual ~Scene();

private:
    CardTable *table;
    QGraphicsPixmapItem *foreground;
    WinCountLabel *win_count_label;
    QGraphicsPixmapItem *tutorial;
    BottomButton *exit_button;
    BottomButton *tutorial_button;
    BottomButton *new_game_button;
    QGraphicsTextItem *tips;

    QMediaPlayer *player;

    QString password;
    QByteArray iv;
    static uint32_t win_count;

    bool move_pressed;
    double scale_factor;
    QPointF move_begin_point;

protected:
    void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;
    void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override;
    void wheelEvent(QGraphicsSceneWheelEvent *event) override;
    void keyPressEvent(QKeyEvent *event) override;

signals:
    void windowMoved(QPoint);
    void scaleFactorChanged(double);
    void exitClicked();

public slots:
    void slotStartNewGameClicked();
    void slotTutorialClicked();
    void slotGameFinished();

public:
    void setScaleFactor(double factor) { scale_factor = factor; }
    static uint32_t winCount() { return win_count; }

private:
    json loadUserData();
    void saveUserData();
    QString getStoragePath() const;
};

#endif // SCENE_H
